package com.hundun.hddeal.controller;


import com.hundun.hddeal.dao.AdUserMapper;
import com.hundun.hddeal.dao.MtUserMapper;
import com.hundun.hddeal.dao.PayMapper;
import com.hundun.hddeal.entity.*;
import com.hundun.hddeal.response.Result;
import com.hundun.hddeal.response.ResultUtil;
import com.hundun.hddeal.service.AdUserService;
import com.hundun.hddeal.service.InvoiceService;
import com.hundun.hddeal.service.MtUserService;
import com.hundun.hddeal.service.PayService;
import com.hundun.hddeal.service.lmpl.OpenTicketServiceImpl;
import com.hundun.hddeal.util.MD5Util;
import com.hundun.hddeal.util.OrderIdUtil;
import com.hundun.hddeal.util.TokenUtilT;
import com.hundun.hddeal.vo.AdUserVo;
import com.hundun.hddeal.vo.InvoiceVo;
import com.hundun.hddeal.vo.MtUserVo;
import com.hundun.hddeal.vo.RequestPageBean;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import java.util.Map;
import static org.assertj.core.util.DateUtil.now;

/**
 * 充值支付
 * @author 谢
 */
@Slf4j
@RestController
@RequestMapping("pay")
public class PayController {

    @Autowired
    AdUserService adUserService;
    @Autowired
    MtUserService mtUserService;
    @Autowired
    PayMapper payMapper;
    @Autowired
    MtUserMapper mtUserMapper;
    @Autowired
    AdUserMapper adUserMapper;
    @Autowired
    PayService payService;
    @Autowired
    InvoiceService invoiceService;
    @Autowired
    OpenTicketServiceImpl openTicketService;


    /**
     * 广告用户充值和提现
     * @param adUserVo
     * @return
     */
    @PostMapping("ad")
    public Result topUp(@RequestBody AdUserVo adUserVo){
        String adUserId = TokenUtilT.getTokenById(adUserVo.getAdUserToken());
        AdUser adUser = adUserService.findAdUserById(adUserId);
        if (adUser!=null) {
            //生成订单号
            String orderId = OrderIdUtil.OrderNum();
            Pay pay = new Pay();
            pay.setMoney(adUserVo.getMoney());
            pay.setUserNames(adUser.getAdId());
            pay.setUserPhone(adUser.getAdPhone());
            pay.setCreateDate(now());
            pay.setPayWay(adUserVo.getPayWay());
            pay.setPayNum(orderId);
            pay.setStatus((byte)1);
            pay.setOrderType(adUserVo.getOrderType());
            pay.setUserFz(adUser.getFz());
            pay.setUserName(adUser.getUserName());
            payMapper.insertSelective(pay);
            return ResultUtil.success(100,"充值操作成功,待平台确认",pay);

            }
        return ResultUtil.error();
        }




    /**
     * 媒体用户充值提现
     * @param mtUserVo
     * @return
     */
    @PostMapping("mt")
    public Result topUp(@RequestBody MtUserVo mtUserVo) {
        String mtUserId = TokenUtilT.getTokenById(mtUserVo.getMtUserToken());
        MtUser mtUser = mtUserService.selectByMtUserId(mtUserId);
        try {
            if (mtUser != null) {
                // 用户账户余额
                double balance = mtUser.getMtMoney();
                //操作金额
                double money = mtUserVo.getMoney();
                //生成订单号
                String orderId = OrderIdUtil.OrderNum();
                    if (money > balance) {
                        return ResultUtil.error(101, "提现操作失败，账户余额不足，请核对");
                    }
                    //mtUser.getTakePwd().equals(mtUserVo.getTakePwd())
                    if (!MD5Util.getSaltverifyMD5(mtUserVo.getTakePwd(), mtUser.getTakePwd())) {
                        return ResultUtil.error("提现密码不对！");
                    }
                    log.info(mtUserVo.getPayWay());
                    Pay pay1 = new Pay();
                    pay1.setMoney(mtUserVo.getMoney());
                    pay1.setUserNames(mtUser.getMtId());
                    pay1.setUserPhone(mtUser.getMtPhone());
                    pay1.setStatus(mtUserVo.getStatus());
                    pay1.setCreateDate(now());
                    pay1.setPayNum(orderId);
                    pay1.setBankPlace(mtUserVo.getBankPlace());
                    pay1.setPayWay(mtUserVo.getPayWay());
                    pay1.setName(mtUserVo.getName());
                    pay1.setBankNum(mtUserVo.getBankNum());
                    pay1.setAlipayNum(mtUserVo.getAlipayNum());
                    pay1.setStatus((byte) 2);
                    pay1.setCompany(mtUserVo.getCompany());
                    pay1.setOrderType(mtUserVo.getOrderType());
                    pay1.setDitchFz(mtUser.getFz());
                    pay1.setDitchName(mtUser.getUserName());
                    payMapper.insertSelective(pay1);
                    mtUserMapper.updateMoney(mtUserId, money);
                    return ResultUtil.success("提现操作成功,待平台确认");
                }
            return ResultUtil.error("不存在该用户");
        } catch (Exception e) {
            e.printStackTrace();
            return ResultUtil.error();
        }

    }

    /**
     * 查询提现列表(媒体)
     * @param pageBean
     * @return
     */
    @PostMapping("getAll")
    public Result getAll(@RequestBody RequestPageBean pageBean) {
            String mttoken = pageBean.getMtUserToken();
            String mtUserId = TokenUtilT.getTokenById(mttoken);
            MtUser mtUser = mtUserService.selectByMtUserId(mtUserId);
            if (mtUser != null) {
                return payService.selectByName(pageBean.getPageNum(), pageBean.getPageSize()
                        , mtUserId, pageBean.getStartTime(), pageBean.getEndTime(),(byte)2);
            }
            return ResultUtil.error("无token");

    }

    /**
     * 查询充值列表(广告)
     */
    @PostMapping("getAdAll")
    public Result getAdAll(@RequestBody RequestPageBean pageBean){
            String adtoken=pageBean.getAdUserToken();
            String adUserId = TokenUtilT.getTokenById(adtoken);
            AdUser adUser =adUserService.findAdUserById(adUserId);
            if(adUser!=null){
                return payService.selectByName(pageBean.getPageNum(),pageBean.getPageSize()
                        ,adUserId,pageBean.getStartTime(),pageBean.getEndTime(),(byte)1);
            }
            return ResultUtil.error("无token");

    }


    /**
     * 添加用户发票信息（媒体）
     * @param invoiceVo
     * @return
     */
    @PostMapping("addTicket")
    public Result addPiao(@RequestBody InvoiceVo invoiceVo){
        String mtUserId = TokenUtilT.getTokenById(invoiceVo.getMtUserToken());
        MtUser mtUser = mtUserService.selectByMtUserId(mtUserId);
        if(mtUser!=null){
            Invoice invoice=new Invoice();
            invoice.setCardFirst(invoiceVo.getCardFirst());
            invoice.setCardNum(invoiceVo.getCardNum());
            invoice.setCardSecond(invoiceVo.getCardSecond());
            invoice.setCompany(invoiceVo.getCompany());
            invoice.setCooperation(invoiceVo.getCooperation());
            invoice.setIdCard(invoiceVo.getIdCard());
            invoice.setIsTicket(invoiceVo.getIsTicket());
            invoice.setOpenBank(invoiceVo.getOpenBank());
            invoice.setOpenCity(invoiceVo.getOpenCity());
            invoice.setOpenProvince(invoiceVo.getOpenProvince());
            invoice.setPayType(invoiceVo.getPayType());
            invoice.setPayUser(invoiceVo.getPayUser());
            invoice.setPhone(invoiceVo.getPhone());
            invoice.setMtId(mtUserId);
            return invoiceService.insertSelective(invoice);
        }
        return ResultUtil.error("无token");
    }

    /**
     * 获取用户是否有开票信息(媒体)
     * @param map
     * @return
     */
    @PostMapping("getOneTicket")
    public Result getOneTicket(@RequestBody Map<String ,String> map){
        String token=map.get("mtUserToken");
        String mtUserId = TokenUtilT.getTokenById(token);
        MtUser mtUser =mtUserService.selectByMtUserId(mtUserId);
        if(mtUser!=null){
            return  invoiceService.selectGetuser(mtUserId);
        }
        return ResultUtil.error("无token");
    }

    /**
     * 修改用户开票信息
     * @param invoiceVo
     * @return
     */
    @PostMapping("updateTicket")
    public Result updateTicket(@RequestBody InvoiceVo invoiceVo){
        String mtUserId = TokenUtilT.getTokenById(invoiceVo.getMtUserToken());
        MtUser mtUser = mtUserService.selectByMtUserId(mtUserId);
        if(mtUser!=null){
            Invoice invoice=new Invoice();
            invoice.setCardFirst(invoiceVo.getCardFirst());
            invoice.setCardNum(invoiceVo.getCardNum());
            invoice.setCardSecond(invoiceVo.getCardSecond());
            invoice.setCompany(invoiceVo.getCompany());
            invoice.setCooperation(invoiceVo.getCooperation());
            invoice.setIdCard(invoiceVo.getIdCard());
            invoice.setIsTicket(invoiceVo.getIsTicket());
            invoice.setOpenBank(invoiceVo.getOpenBank());
            invoice.setOpenCity(invoiceVo.getOpenCity());
            invoice.setOpenProvince(invoiceVo.getOpenProvince());
            invoice.setPayType(invoiceVo.getPayType());
            invoice.setPayUser(invoiceVo.getPayUser());
            invoice.setPhone(invoiceVo.getPhone());
            invoice.setMtId(mtUserId);
            return invoiceService.updateByPrimaryKeySelective(invoice);
        }
        return ResultUtil.error("无token");
    }




    /**
     * 媒体用户添加发票
     * @param invoiceVo
     * @return
     */
  /*  @PostMapping("addOpenTicket")
    public Result addOpenTicket(@RequestBody InvoiceVo invoiceVo){
        String mtUserId = TokenUtilT.getTokenById(invoiceVo.getMtUserToken());
        MtUser mtUser = mtUserService.selectByMtUserId(mtUserId);
        if(mtUser!=null){
            OpenTicket openTicket =new OpenTicket();
            openTicket.setBankNum(invoiceVo.getCardNum());
            openTicket.setCompanyName(invoiceVo.getCompany());
            openTicket.setExtractMoney(invoiceVo.getExtractMoney());
            openTicket.setMtId(mtUserId);
            openTicket.setOpenBank(invoiceVo.getOpenBank());
            openTicket.setTaxMoney(invoiceVo.getTaxMoney());
            openTicketService.insertSelective(openTicket);
        }
        return ResultUtil.error("无token");

    }*/








}

